home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODMisc / Include / FWFxMath.h < prev    next >
Encoding:
Text File  |  1996-08-16  |  12.2 KB  |  449 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFxMath.h
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWFXMATH_H
  11. #define FWFXMATH_H
  12.  
  13. #ifndef FWENVDEF_H
  14. #include "FWEnvDef.h"
  15. #endif
  16.  
  17. #ifndef _ODMATH_
  18. #include <ODMath.h>
  19. #endif
  20.  
  21. //========================================================================================
  22. // Definitions and utilities
  23.  
  24. // These have to be done as macros because Metrowerks C++ 1.2.2 has difficulties
  25. //    with inlines that call other inlines
  26.  
  27. #define FW_PrivIntToFixed(i)            (ODFixed)((long) (i) << 16)
  28. #define FW_PrivDoubleToFixed(d)            (ODFixed)((double) (d) * 65536.0)
  29.  
  30. #define FW_PrivFixedToInt(f)            (int)((ODFixed)((f) + 0x00008000) >> 16)
  31. #define FW_PrivFixedToTruncatedInt(f)    (int)((ODFixed)((f) + 0x00000010) >> 16)
  32.  
  33. #define FW_PrivFixedToDouble(f)            (double)((ODFixed)(f) / 65536.0)
  34.  
  35. //========================================================================================
  36. // SOM fixed math types
  37. //========================================================================================
  38.  
  39. struct FW_Fixed
  40. {
  41.     ODFixed    fRep;
  42. };
  43.  
  44. struct FW_Wide
  45. {
  46.     ODWide    fRep;
  47. };
  48.  
  49. //========================================================================================
  50. // Forward class declaration
  51. //========================================================================================
  52.  
  53. class FW_CWritableStream;
  54. class FW_CReadableStream;
  55.  
  56. //========================================================================================
  57. //    Operators
  58. //========================================================================================
  59.  
  60. // ----- Stream I/O operators -----
  61. // No operators for wide numbers: those are intended to be used only as temporaries
  62.  
  63. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_Fixed& fx);
  64. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_Fixed& fx);
  65.  
  66. // ----- Operators: FW_Fixed -----
  67.  
  68. FW_Boolean            operator ==    (FW_Fixed f1, FW_Fixed f2);
  69. FW_Boolean            operator !=    (FW_Fixed f1, FW_Fixed f2);
  70.  
  71. FW_Boolean            operator >    (FW_Fixed f1, FW_Fixed f2);
  72. FW_Boolean            operator <    (FW_Fixed f1, FW_Fixed f2);
  73. FW_Boolean            operator >=    (FW_Fixed f1, FW_Fixed f2);
  74. FW_Boolean            operator <=    (FW_Fixed f1, FW_Fixed f2);
  75.  
  76. FW_Fixed            operator +    (FW_Fixed f1, FW_Fixed f2);
  77. FW_Fixed            operator -    (FW_Fixed f1, FW_Fixed f2);
  78. FW_Fixed            operator *    (FW_Fixed f1, FW_Fixed f2);
  79. FW_Fixed            operator /    (FW_Fixed f1, FW_Fixed f2);
  80.  
  81. FW_Fixed&            operator +=    (FW_Fixed& f1, FW_Fixed f2);
  82. FW_Fixed&            operator -=    (FW_Fixed& f1, FW_Fixed f2);
  83. FW_Fixed&            operator *=    (FW_Fixed& f1, FW_Fixed f2);
  84. FW_Fixed&            operator /=    (FW_Fixed& f1, FW_Fixed f2);
  85.  
  86. FW_Fixed            operator -  (FW_Fixed f);
  87.  
  88. FW_Fixed&            operator ++    (FW_Fixed& f);                // prefix
  89. void                operator ++    (FW_Fixed& f, int i);        // postfix
  90. FW_Fixed&            operator --    (FW_Fixed& f);                // prefix
  91. void                 operator --    (FW_Fixed& f, int i);        // postfix
  92.  
  93. // ----- Special cases -----
  94.  
  95. FW_Fixed            FW_Half(FW_Fixed f);
  96. FW_Fixed            FW_MultipliedByInt(FW_Fixed f, short i);
  97. FW_Fixed            FW_DividedByInt(FW_Fixed f, int i);
  98.  
  99. FW_Fixed            FW_RoundedToInt(FW_Fixed f);
  100.  
  101. // ----- Transcendental functions -----
  102.  
  103. FW_Fixed            FW_Sin(FW_Fixed f);
  104. FW_Fixed            FW_Cos(FW_Fixed f);
  105. FW_Fixed            FW_Sqrt(FW_Fixed f);
  106.  
  107. // ----- Conversions -----
  108.  
  109. #define FW_FixedToInt(f)            FW_PrivFixedToInt((f).fRep)
  110. #define FW_FixedToTruncatedInt(f)    FW_PrivFixedToTruncatedInt((f).fRep)
  111. #define FW_FixedToDouble(f)            FW_PrivFixedToDouble((f).fRep)
  112. #define FW_FixedToODFixed(f)        ((f).fRep)
  113.  
  114. FW_Fixed            FW_ODFixedToFixed(ODFixed f);
  115. FW_Fixed            FW_IntToFixed(int i);
  116. FW_Fixed            FW_DoubleToFixed(double d);
  117.  
  118. //========================================================================================
  119. // Conversions
  120. //========================================================================================
  121.  
  122. inline FW_Fixed FW_ODFixedToFixed(ODFixed f)
  123. {
  124.     FW_Fixed fx;
  125.     fx.fRep = f;
  126.     return fx;
  127. }
  128.  
  129. inline FW_Fixed FW_IntToFixed(int i)
  130. {
  131.     return FW_ODFixedToFixed(FW_PrivIntToFixed(i));
  132. }
  133.  
  134. inline FW_Fixed FW_DoubleToFixed(double d)
  135. {
  136.     return FW_ODFixedToFixed(FW_PrivDoubleToFixed(d));
  137. }
  138.  
  139. //========================================================================================
  140. // Math with assignment
  141. //========================================================================================
  142.  
  143. inline FW_Fixed& operator += (FW_Fixed& f1, FW_Fixed f2)
  144. {
  145.     f1.fRep += f2.fRep;
  146.     return f1;
  147. }
  148.  
  149. inline FW_Fixed& operator -= (FW_Fixed& f1, FW_Fixed f2)
  150. {
  151.     f1.fRep -= f2.fRep;
  152.     return f1;
  153. }
  154.  
  155. inline FW_Fixed& operator *= (FW_Fixed& f1, FW_Fixed f2)
  156. {
  157.     f1.fRep = ODFixedMultiply(f1.fRep, f2.fRep);
  158.     return f1;
  159. }
  160.  
  161. inline FW_Fixed& operator/=(FW_Fixed& f1, FW_Fixed f2)
  162. {
  163.     f1.fRep = ODFixedDivide(f1.fRep, f2.fRep);
  164.     return f1;
  165. }
  166.  
  167. //========================================================================================
  168. // Special math cases
  169. //========================================================================================
  170.  
  171. inline FW_Fixed FW_Half(FW_Fixed f)
  172. {
  173.     return FW_ODFixedToFixed(f.fRep / 2);
  174. }
  175.  
  176. inline FW_Fixed FW_MultipliedByInt(FW_Fixed f, short i)
  177. {
  178.     return FW_ODFixedToFixed(f.fRep * i);
  179. }
  180.  
  181. inline FW_Fixed FW_DividedByInt(FW_Fixed f, int i)
  182. {
  183.     return FW_ODFixedToFixed(f.fRep / i);
  184. }
  185.  
  186. inline FW_Fixed FW_RoundedToInt(FW_Fixed f)
  187. {
  188.     return FW_ODFixedToFixed((f.fRep + 0x00008000) & 0xFFFF0000ul);
  189. }
  190.  
  191. //========================================================================================
  192. // Increment/decrement
  193. //========================================================================================
  194.  
  195. inline FW_Fixed& operator++(FW_Fixed& f)
  196. {
  197.     f.fRep += FW_PrivIntToFixed(1);
  198.     return f;
  199. }
  200.  
  201. inline void operator++(FW_Fixed& f, int /* i */)
  202. {
  203.     f.fRep += FW_PrivIntToFixed(1);
  204. }
  205.  
  206. inline FW_Fixed& operator--(FW_Fixed& f)
  207. {
  208.     f.fRep -= FW_PrivIntToFixed(1);
  209.     return f;
  210. }
  211.  
  212. inline void operator--(FW_Fixed& f, int /* i */)
  213. {
  214.     f.fRep -= FW_PrivIntToFixed(1);
  215. }
  216.  
  217. //========================================================================================
  218. // Math
  219. //========================================================================================
  220.  
  221. inline FW_Fixed operator-(FW_Fixed f)
  222. {
  223.     FW_Fixed r;
  224.     r.fRep = -f.fRep;
  225.     return r;
  226. }
  227.  
  228. #ifndef FW_DEBUG
  229.  
  230. inline FW_Fixed operator+(FW_Fixed f1, FW_Fixed f2)
  231. {
  232.     return FW_ODFixedToFixed(f1.fRep + f2.fRep);
  233. }
  234.  
  235. inline FW_Fixed operator-(FW_Fixed f1, FW_Fixed f2)
  236. {
  237.     return FW_ODFixedToFixed(f1.fRep - f2.fRep);
  238. }
  239.  
  240. inline FW_Fixed operator*(FW_Fixed f1, FW_Fixed f2)
  241. {
  242.     return FW_ODFixedToFixed(ODFixedMultiply(f1.fRep, f2.fRep));
  243. }
  244.  
  245. inline FW_Fixed operator/(FW_Fixed f1, FW_Fixed f2)
  246. {
  247.     return FW_ODFixedToFixed(ODFixedDivide(f1.fRep, f2.fRep));
  248. }
  249.  
  250. #endif
  251.  
  252. //========================================================================================
  253. // Comparisons
  254. //========================================================================================
  255.  
  256. inline FW_Boolean operator>(FW_Fixed f1, FW_Fixed f2)
  257. {
  258.     return f1.fRep > f2.fRep;
  259. }
  260.  
  261. inline FW_Boolean operator==(FW_Fixed f1, FW_Fixed f2)
  262. {
  263.     return f1.fRep == f2.fRep;
  264. }
  265.  
  266. inline FW_Boolean operator<(FW_Fixed f1, FW_Fixed f2)
  267. {
  268.     return f1.fRep < f2.fRep;
  269. }
  270.  
  271. inline FW_Boolean operator>=(FW_Fixed f1, FW_Fixed f2)
  272. {
  273.     return f1.fRep >= f2.fRep;
  274. }
  275.  
  276. inline FW_Boolean operator!=(FW_Fixed f1, FW_Fixed f2)
  277. {
  278.     return f1.fRep != f2.fRep;
  279. }
  280.  
  281. inline FW_Boolean operator<=(FW_Fixed f1, FW_Fixed f2)
  282. {
  283.     return f1.fRep <= f2.fRep;
  284. }
  285.  
  286. //========================================================================================
  287. // FW_Wide
  288. //========================================================================================
  289.  
  290. // ----- Conversions -----
  291.  
  292. FW_Wide                FW_IntToWide(int i);
  293. FW_Wide                FW_FixedToWide(FW_Fixed f);
  294. FW_Wide                FW_ODFixedToWide(ODFixed f);
  295. FW_Wide                FW_ODWideToWide(const ODWide& w);
  296.  
  297. int                    FW_WideToInt(const FW_Wide& w);
  298. ODFixed                FW_WideToODFixed(const FW_Wide& w);
  299. inline FW_Fixed        FW_WideToFixed(const FW_Wide& w);
  300.  
  301. // ----- Comparisons -----
  302.  
  303. FW_Boolean            operator ==    (const FW_Wide& w1, const FW_Wide& w2);
  304. FW_Boolean            operator !=    (const FW_Wide& w1, const FW_Wide& w2);
  305.  
  306. FW_Boolean            operator >    (const FW_Wide& w1, const FW_Wide& w2);
  307. FW_Boolean            operator <    (const FW_Wide& w1, const FW_Wide& w2);
  308. FW_Boolean            operator >=    (const FW_Wide& w1, const FW_Wide& w2);
  309. FW_Boolean            operator <=    (const FW_Wide& w1, const FW_Wide& w2);
  310.  
  311. // ----- Math -----
  312.  
  313. FW_Wide                 operator +    (const FW_Wide& w1, const FW_Wide& w2);
  314. FW_Wide                 operator -    (const FW_Wide& w1, const FW_Wide& w2);
  315.  
  316. FW_Wide&            operator +=    (FW_Wide& w1, const FW_Wide& w2);
  317. FW_Wide&            operator -=    (FW_Wide& w1, const FW_Wide& w2);
  318.  
  319. #ifdef FW_DEBUG
  320. FW_Wide&            operator +=    (FW_Wide& w1, const FW_Fixed& f2);
  321. FW_Wide&            operator -=    (FW_Wide& w1, const FW_Fixed& f2);
  322. #endif
  323.  
  324. FW_Wide                 FW_WideMultiply    (FW_Fixed f1, FW_Fixed f2);
  325. FW_Fixed            operator /  (const FW_Wide& w1, FW_Fixed f2);
  326.  
  327. FW_Fixed            FW_Sqrt(const FW_Wide& w);
  328.  
  329. //========================================================================================
  330. // Conversions
  331. //========================================================================================
  332.  
  333. inline FW_Wide FW_ODWideToWide(const ODWide& w)
  334. {
  335.     FW_Wide sw;
  336.     sw.fRep = w;
  337.     return sw;    
  338. }
  339.  
  340. //========================================================================================
  341. // Math
  342. //========================================================================================
  343.  
  344. inline FW_Wide& operator += (FW_Wide& w1, const FW_Wide& w2)
  345. {
  346.     ODWideAdd(&w1.fRep, &w2.fRep);
  347.     return w1;
  348. }
  349.  
  350. inline FW_Wide& operator -= (FW_Wide& w1, const FW_Wide& w2)
  351. {
  352.     ODWideSubtract(&w1.fRep, &w2.fRep);
  353.     return w1;
  354. }
  355.  
  356. //========================================================================================
  357. // Comparison
  358. //========================================================================================
  359.  
  360. inline FW_Boolean operator ==    (const FW_Wide& w1, const FW_Wide& w2)
  361. {
  362.     return ODWideCompare(&w1.fRep, &w2.fRep) == 0;
  363. }
  364.  
  365. inline FW_Boolean operator !=    (const FW_Wide& w1, const FW_Wide& w2)
  366. {
  367.     return ODWideCompare(&w1.fRep, &w2.fRep) != 0;
  368. }
  369.  
  370. inline FW_Boolean operator >    (const FW_Wide& w1, const FW_Wide& w2)
  371. {
  372.     return ODWideCompare(&w1.fRep, &w2.fRep) > 0;
  373. }
  374.  
  375. inline FW_Boolean operator <    (const FW_Wide& w1, const FW_Wide& w2)
  376. {
  377.     return ODWideCompare(&w1.fRep, &w2.fRep) < 0;
  378. }
  379.  
  380. inline FW_Boolean operator >=    (const FW_Wide& w1, const FW_Wide& w2)
  381. {
  382.     return ODWideCompare(&w1.fRep, &w2.fRep) >= 0;
  383. }
  384.  
  385. inline FW_Boolean operator <=    (const FW_Wide& w1, const FW_Wide& w2)
  386. {
  387.     return ODWideCompare(&w1.fRep, &w2.fRep) <= 0;
  388. }
  389.  
  390. //========================================================================================
  391. // Min/Max/Abs utilities
  392. //========================================================================================
  393.  
  394. //----------------------------------------------------------------------------------------
  395. //    FW_Fixed
  396. //----------------------------------------------------------------------------------------
  397.  
  398. inline FW_Fixed FW_Minimum(FW_Fixed f1, FW_Fixed f2)
  399. {
  400.     return f1.fRep < f2.fRep ? f1 : f2;
  401. }
  402.  
  403. inline FW_Fixed FW_Maximum(FW_Fixed f1, FW_Fixed f2)
  404. {
  405.     return f1.fRep > f2.fRep ? f1 : f2;
  406. }
  407.  
  408. inline FW_Fixed FW_Absolute(FW_Fixed f)
  409. {
  410.     return FW_ODFixedToFixed(f.fRep > 0 ? f.fRep : -f.fRep);
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. //    FW_Wide
  415. //----------------------------------------------------------------------------------------
  416.  
  417. inline FW_Wide FW_Minimum(const FW_Wide& w1, const FW_Wide& w2)
  418. {
  419.     return w1 < w2 ? w1 : w2;
  420. }
  421.  
  422. inline FW_Wide FW_Maximum(const FW_Wide& w1, const FW_Wide& w2)
  423. {
  424.     return w1 > w2 ? w1 : w2;
  425. }
  426.  
  427. inline FW_Fixed FW_WideToFixed(const FW_Wide& w1)
  428. {
  429.     return FW_ODFixedToFixed(FW_WideToODFixed(w1));
  430. }
  431.  
  432. //========================================================================================
  433. // Global constants
  434. //========================================================================================
  435.  
  436. extern const FW_Fixed FW_kFixed0;        // 0
  437. extern const FW_Fixed FW_kFixedPos1;    // +1
  438. extern const FW_Fixed FW_kFixedNeg1;    // -1
  439. extern const FW_Fixed FW_kFixed72;        // 72
  440. extern const FW_Fixed FW_kFixedPI;        // pi
  441.  
  442. extern const FW_Wide FW_kWide0;            // 0
  443. extern const FW_Wide FW_kWidePos1;        // +1
  444. extern const FW_Wide FW_kWideNeg1;        // -1
  445. extern const FW_Wide FW_kWide72;        // 72
  446. extern const FW_Wide FW_kWidePI;        // pi
  447.  
  448. #endif
  449.